
package authoritah.ui.event;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;


import authoritah.db.EventType;
import authoritah.ui.common.renderers.MemberComboboxTableCellRenderer;
import authoritah.ui.common.renderers.TimeTableCellRenderer;
import authoritah.utils.SpringUtilities;

import com.toedter.calendar.JDateChooser;

public class EventAttendanceView extends JPanel {
	private EventAttendanceModel model;
	
	private JTable attendanceInputTable;
	private JScrollPane attendanceInputScroll;
	
	private JTextArea copyPasteField;
	private JScrollPane copyPasteScroll;
	
	private JPanel eventDetails;
	private JTextField eventName;
	private JComboBox eventType;
	private JDateChooser date;
	private JTextField startTime;
	private JTextField endTime;
	private JTextField maxPoints;
	
	private JPanel buttonPanel;
	private JButton parseTextField;
	private JButton saveAttendance;
	
	public EventAttendanceView(EventAttendanceModel model) {
		super();
		this.model = model;
		
		initComponents();
		layoutComponents();
	}
	
	private void initComponents() {
		attendanceInputTable = new JTable(model.getInputTableModel());
		
		TableColumnModel tcm = attendanceInputTable.getColumnModel();
	    TableColumn tc = tcm.getColumn(0);
	    tc.setCellRenderer(new MemberComboboxTableCellRenderer());
	    tc.setCellEditor(new MemberComboboxTableCellRenderer());
	    
	    tc = tcm.getColumn(1);
	    tc.setCellRenderer(new TimeTableCellRenderer());
	    
	    tc = tcm.getColumn(2);
	    tc.setCellRenderer(new TimeTableCellRenderer());
		
		attendanceInputScroll = new JScrollPane(attendanceInputTable);
		
		eventName = new JTextField(model.getEventNameDocument(), "", 20);
		model.setEventNameDocument(eventName.getDocument());
		
		eventType = new JComboBox(EventType.getAllEventTypes().toArray());
		eventType.setPreferredSize(new Dimension(150,20));
		model.setEventTypeModel(eventType.getModel());
		
		startTime = new JTextField("", 5);
		model.setStartDocument(startTime.getDocument());
		
		date = new JDateChooser();
		model.setDateEditor(date.getDateEditor());
		
		endTime = new JTextField("", 5);
		model.setEndDocument(endTime.getDocument());
		
		maxPoints = new JTextField(model.getMaxPointsDocument(), "", 2);
		model.setMaxPointsDocument(maxPoints.getDocument());
		
		copyPasteField = new JTextArea();
		copyPasteScroll = new JScrollPane(copyPasteField);
		model.setCopyPasteDocument(copyPasteField.getDocument());
		
		eventDetails = new JPanel(new SpringLayout());
		eventDetails.add(new JLabel("Event Type"));
		eventDetails.add(eventType);
		eventDetails.add(new JLabel("Event Name"));
		eventDetails.add(eventName);
		eventDetails.add(new JLabel("Maximum Points"));
		eventDetails.add(maxPoints);
		eventDetails.add(new JLabel("Date"));
		eventDetails.add(date);
		eventDetails.add(new JLabel("Start Time"));
		eventDetails.add(startTime);
		eventDetails.add(new JLabel("End Time"));
		eventDetails.add(endTime);
		
		SpringUtilities.makeCompactGrid(eventDetails, 2, 6, 0, 0, 10, 10);
		
		buttonPanel = new JPanel(new GridLayout(1,2));
		parseTextField = new JButton();
		saveAttendance = new JButton();
	}
	
	private void layoutComponents() {
		buttonPanel.add(parseTextField);
		buttonPanel.add(saveAttendance);
		
		JPanel bottomPanel = new JPanel(new GridLayout(1,2));
		bottomPanel.add(copyPasteScroll);
		bottomPanel.add(attendanceInputScroll);
		
		this.setLayout(new BorderLayout());
		this.add(eventDetails, BorderLayout.NORTH);
		this.add(bottomPanel, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
	}
	
	public void setParseAction(Action action) {
		parseTextField.setAction(action);
	}
	
	public void setSaveAction(Action action) {
		saveAttendance.setAction(action);
	}
	
	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		model.reset();
	}
}
